package com.yunxi.service.localService.impl.otd;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yunxi.core.web.ApiResult;
import com.yunxi.core.web.ResponseUtil;
import com.yunxi.model.otd.OtdOrderCirWeek;
import com.yunxi.model.otd.OtdOrderMonth;
import com.yunxi.model.otd.OtdOrderMonthVersion;
import com.yunxi.service.localService.otd.OtdOrderCirWeekService;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.yunxi.core.service.ServiceImplPlus;
import com.yunxi.dao.otd.OtdOrderCirWeekVersionMapper;
import com.yunxi.model.otd.OtdOrderCirWeekVersion;
import com.yunxi.service.localService.otd.OtdOrderCirWeekVersionService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * <p>
 * 周CIR预测版本管理 服务实现类
 * </p>
 *
 * @author XUQI
 * @since 2019-10-14
 */
@Service
public class OtdOrderCirWeekVersionServiceImpl extends
    ServiceImplPlus<OtdOrderCirWeekVersionMapper, OtdOrderCirWeekVersion> implements OtdOrderCirWeekVersionService {

    @Autowired
    @Qualifier("otdOrderCirWeekServiceImpl")
    OtdOrderCirWeekService cirWeekService;

    /**
     * 删除版本
     *
     * @param ids ids
     * @return ApiResult
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ApiResult removeByIds(List<String> ids) {
        // 删除周预测
        LambdaQueryWrapper<OtdOrderCirWeek> monthWrapper = new LambdaQueryWrapper<>();
        monthWrapper.in(OtdOrderCirWeek::getVersionId, ids);
        cirWeekService.remove(monthWrapper);

        // 删除版本记录
        int delCount = baseMapper.deleteBatchIds(ids);
        if (delCount != ids.size()) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseUtil.fail(-1, "请稍后重试");
        }
        return ResponseUtil.ok();
    }


    /**
     * 设置默认版本
     *
     * @param id id
     * @return ApiResult
     */
    @Override
    public ApiResult setDefault(String id) {
        // 取消原有默认版本
        OtdOrderCirWeekVersion cancelVersion = new OtdOrderCirWeekVersion();
        cancelVersion.setIsDefault(0);
        LambdaQueryWrapper<OtdOrderCirWeekVersion> cancelWrapper = new LambdaQueryWrapper<>();
        cancelWrapper.eq(OtdOrderCirWeekVersion::getIsDefault, 1);
        baseMapper.update(cancelVersion, cancelWrapper);

        //根据ID设置位默认版本
        OtdOrderCirWeekVersion setVersion = new OtdOrderCirWeekVersion();
        setVersion.setIsDefault(1);
        LambdaQueryWrapper<OtdOrderCirWeekVersion> setWrapper = new LambdaQueryWrapper<>();
        setWrapper.eq(OtdOrderCirWeekVersion::getId,id);
        if (baseMapper.update(setVersion, setWrapper) < 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return ResponseUtil.fail(-1, "设置失败请稍后重试！");
        }
        return ResponseUtil.ok();
    }
}
